<template>
  <div class="player">
    <div class="player_content">
      <div class="logo">
        <div class="logo_left">
          <img src="../../assets/logo.png" alt="" @click="$router.push('/')" />
        </div>
        <div class="logo_right">
          <img src="../../assets/images/no-login.png" alt="" />
          <span>名字</span>
        </div>
      </div>
      <div class="w player_main">
        <!-- 标志 -->
        <!-- <div class="player_main_flag">
          <ul>
            <li>默认播放列表</li>
            <li>喜欢的音乐</li>
            <li>我的歌单</li>
          </ul>
        </div> -->
        <!-- 歌曲列表和 歌词的盒子 -->
        <div class="player_main_content">
          <!-- 左边是  歌曲列表 -->
          <div class="player_main_content_songsList">
            <!-- 添加到歌单 和 删除 按钮-->
            <div class="player_main_content_songsList_button">
              <button>
                <i class="iconfont icon-tianjiawenjian"></i>
                添加到歌单
              </button>
              <button>
                <i class="iconfont icon-shanchu"></i>
                删除
              </button>
            </div>
            <!-- 歌曲列表 -->
            <div class="player_main_content_songsList_list">
              <el-checkbox-group v-model="checkList">
                <div class="list_item">
                  <!-- 选择框 -->
                  <div class="list_item_checkbox">
                    <el-checkbox label="复选框 A"></el-checkbox>
                  </div>
                  <!-- 索引号 -->
                  <div class="list_item_index"></div>
                  <!-- 歌曲 -->
                  <div class="list_item_name">歌曲</div>
                  <!-- 歌手 -->
                  <div class="list_item_author">歌手</div>
                  <!-- 时长 -->
                  <div class="list_item_time">时长</div>
                </div>
                <div
                  class="list_item"
                  v-for="(item, index) in $store.state.playerStore.songsList"
                  :key="item.id"
                >
                  <!-- 选择框 -->
                  <div class="list_item_checkbox">
                    <el-checkbox label="复选框 A"></el-checkbox>
                    <img
                      src="../../assets/images/playing.gif"
                      alt=""
                      style="margin-left: 10px"
                      v-if="
                        index === $store.state.playerStore.currentSongIndex &&
                        isPlaying == true
                      "
                    />
                  </div>
                  <!-- 索引号 -->
                  <div class="list_item_index">
                    <span>{{
                      index + 1 &lt; 10 ? "0" + (index + 1) : index + 1
                    }}</span>
                    <i class="iconfont icon-bofang" @click="playSong(item)"></i>
                  </div>
                  <!-- 歌曲 -->
                  <div class="list_item_name one_txt_cut">{{ item.name }}</div>
                  <!-- 歌手 -->
                  <div class="list_item_author one_txt_cut">
                    <span v-for="itemm in item.ar" :key="itemm.id">
                      {{ itemm.name }}
                    </span>
                  </div>
                  <!-- 时长 -->
                  <div class="list_item_time">
                    <span> {{ formatDuration(item.dt) }} </span>
                    <div>
                      <i class="iconfont icon-tianjiawenjian"></i>
                      <i class="iconfont icon-shanchu"></i>
                    </div>
                  </div>
                </div>
              </el-checkbox-group>
            </div>
          </div>
          <!-- 右边是歌词 -->
          <div class="player_main_content_lyric">
            <div class="player_main_content_lyric_img">
              <GlobalImageDeep
                :url="
                  $store.state.playerStore.currentSong.name
                    ? $store.state.playerStore.currentSong.al.picUrl
                    : 'xxxx'
                "
                :imgWidth="200"
                :imgHeight="200"
              />
            </div>
            <p class="one_txt_cut songs_name">
              歌曲名:
              <span>{{
                $store.state.playerStore.currentSong.name || "呆呆狗"
              }}</span>
            </p>
            <p class="one_txt_cut">
              歌手名:
              <!-- <span>{{
                $store.state.playerStore.currentSong.name
                  ? formatAuthor($store.state.playerStore.currentSong.ar)
                  : "请等待~"
              }}</span> -->

              <span
                v-for="item in $store.state.playerStore.currentSong.ar"
                :key="item.id"
              >
                {{ item.name }}
              </span>
            </p>
            <div class="player_main_content_lyric_box">
              <div
                class="player_main_content_lyric_box_content"
                :style="{ transform: `translateY(${lyricY}px)` }"
              >
                <p
                  v-for="(item, index) in $store.state.playerStore.lyricList"
                  :key="index"
                  :class="
                    $store.state.playerStore.currentLyricIndex == index
                      ? 'active'
                      : null
                  "
                >
                  {{ item.content == "" ? "……" : item.content }}
                </p>
              </div>
            </div>
          </div>
        </div>
        <!-- 播放  一些的按钮 -->
        <div class="player_main_audio_button">
          <!-- 左边 ，播放暂停、上一首，下一首 -->
          <div class="player_main_audio_button_left">
            <i class="iconfont icon-shangyishou"></i>
            <i
              class="iconfont icon-bofang"
              v-if="!isPlaying"
              @click="playOrpause"
            ></i>
            <i class="iconfont icon-zanting" v-else @click="playOrpause"></i>
            <i class="iconfont icon-xiayishou"></i>
          </div>
          <!-- 歌曲的信息 +　歌曲实时进度 -->
          <div class="player_main_audio_button_center">
            <!-- 歌曲信息 -->
            <div class="player_main_audio_button_center_info">
              <div>
                <span>{{
                  $store.state.playerStore.currentSong.name || "呆呆狗"
                }}</span>
                <i>——</i>
                <!-- <span>{{
                  $store.state.playerStore.currentSong.name
                    ? formatAuthor($store.state.playerStore.currentSong.ar)
                    : "请等待~"
                }}</span> -->
                <span
                  v-for="item in $store.state.playerStore.currentSong.ar"
                  :key="item.id"
                >
                  {{ item.name }}
                </span>
              </div>
              <div>
                <!-- 实时时长 -->
                <span class="realDuration">{{ realDuration }}</span>
                <i>/</i>
                <!-- 总时长 -->
                <span class="totalDuration">{{
                  formatDuration($store.state.playerStore.currentSong.dt)
                }}</span>
              </div>
            </div>
            <!-- 歌曲播放进度的 滑块 -->
            <!-- input 是用户在滑块中 拖拽的时候触发的 -->
            <!-- change是用户在滑块中 点击并且松手 -->
            <el-slider
              v-model="songProgress"
              :show-tooltip="false"
              @change="songProgressChange"
            ></el-slider>
          </div>
          <!-- 音量、喜欢、播放顺序（单曲循环？顺序？） -->
          <div class="player_main_audio_button_right">
            <div class="sequence">
              <!-- 顺序播放？随机播放？单曲循环？ -->
              <i
                class="iconfont icon-shunxu"
                v-if="$store.state.playerStore.sequence === 0"
              ></i>
              <i
                class="iconfont icon-suiji"
                v-if="$store.state.playerStore.sequence === 1"
              ></i>
              <i
                class="iconfont icon-danquxunhuan"
                v-if="$store.state.playerStore.sequence === 2"
              ></i>
              <!-- 是否已经加入了喜欢 和 不喜欢 -->
              <i class="iconfont icon-xihuan"></i>
              <!-- <i class="iconfont icon-xihuan1"></i> -->
              <!-- 静音，非静音 -->
              <i class="iconfont icon-jiadayinliang" v-if="!jingyinFlag"></i>
              <i class="iconfont icon-guanbiyinliang" v-else></i>
            </div>
            <!-- 音量滑块 -->
            <div class="slider">
              <el-slider
                v-model="voiceProgress"
                :show-tooltip="false"
                @input="voiceProgressInput"
                @change="voiceProgressChange"
              ></el-slider>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 大背景的遮罩层 -->
    <div class="player_bg_mask"></div>
    <!-- 大背景 -->
    <div class="player_bg" :style="{ backgroundImage: 'url(' + bgImg + ')' }">
      >
    </div>
    <audio
      :src="songUrl"
      ref="songAudioRef"
      @timeupdate="songAudioUpdate"
      @ended="songAudioEnd"
    ></audio>
  </div>
</template>

<script>
import { getSongsDetails, getSongUrl, getSongLyric } from "../../api/playerApi";
import { parseLyric } from "../../utils/parse-lyric.js";
import moment from "moment";

export default {
  name: "player",
  components: {},
  data() {
    return {
      checkList: [],
      isPlaying: false, // 是否正在播放 true播放 false 暂停，
      voiceProgress: 100, //音量的进度
      songProgress: 0, // 歌曲播放的进度
      realDuration: "00:00", // 实时时间
      dt: "", // 后端返回的 此歌曲的 时长，单位是毫秒
      totalDuration: "00:00", // 总时长
      bgImg: "",
      songUrl: "", // 歌曲的 mp3地址
      songProgressIsChanging: false, // 鼠标在 歌曲进度条上 滑动的时候
      jingyinFlag: false, // false 没静音 true 静音
      lyricY: 0, // 向上偏移值
    };
  },
  async created() {
    // 获取歌曲详情
    await this.getSongDetails([this.$route.params.id]);
    await this.getSongUrl(this.$route.params.id);
    await this.getSongLyric(this.$route.params.id);

    // 其他逻辑
    // this.handeleSong();
  },
  mounted() {},
  beforeDestroy() {
    // 卸载 组件 前
  },

  methods: {
    // 根据 id  获取 歌曲的详情
    async getSongDetails(ids) {
      try {
        const result = await getSongsDetails(ids);
        if (result.code === 200) {
          // console.log("歌曲详情获取成功", result.songs);
          // console.log("页面更新");

          this.bgImg = result.songs[0].al.picUrl;
          this.dt = result.songs[0].dt;
          await this.$store.commit("playerStore/changeSongsList", result.songs);
          document.title = `正在播放:${
            this.$store.state.playerStore.currentSong.name
          } 作者:${this.formatAuthor(
            this.$store.state.playerStore.currentSong.ar || []
          )}   `;
        }
      } catch (error) {}
    },
    // 根据 id 获取 歌曲的 url
    async getSongUrl(id) {
      try {
        const result = await getSongUrl(id);
        if (result.code === 200) {
          this.songUrl = result.data[0].url;
          // console.log("获取到了歌曲的mp3");
        }
      } catch (error) {}
    },
    // 根据 id 获取歌曲的歌词
    async getSongLyric(id) {
      try {
        const result = await getSongLyric(id);
        if (result.code === 200) {
          console.log("进入了判断");
          console.log(parseLyric(result.lrc.lyric));
          // console.log("获取到了歌曲的mp3");
          this.$store.commit(
            "playerStore/changeLyricList",
            parseLyric(result.lrc.lyric)
          );
        }
      } catch (error) {}
    },

    // 点击播放或者暂停
    playOrpause() {
      if (!this.isPlaying) {
        // 播放歌曲
        this.$refs.songAudioRef.play().then(
          () => {},
          (err) => {
            console.log(err);
            this.$message.error("亲,此歌曲音频未找到~~~~");
          }
        );

        // this.songProgressIsChanging = false;
      } else {
        // 暂停
        this.$refs.songAudioRef.pause();
      }
      this.isPlaying = !this.isPlaying;
    },
    // 音频标签 进度 更新事件
    songAudioUpdate(e) {
      // console.log("songAudioUpdate 默认执行一次");
      // console.log(e);
      const currentTime = e.target.currentTime; // 这里返回的是 秒 当前播放到 什么时间了
      // console.log("即将执行", this.songProgressIsChanging);

      // console.log("音频标签进度 更新 ", this.songProgressIsChanging);
      // 用户的鼠标正在  歌词实时进度跳上 拖拽的时候，不需要实时 设置 播放到了哪里
      this.realDuration = moment(currentTime * 1000).format("mm:ss"); // 更新实时进度
      this.songProgress = ((currentTime * 1000) / this.dt) * 100;

      if (this.isPlaying) {
        // 获取歌词
        let i = 0;
        for (; i < this.$store.state.playerStore.lyricList.length; i++) {
          let lyricItem = this.$store.state.playerStore.lyricList[i];
          // console.log(lyricItem.content);
          if (currentTime * 1000 < lyricItem.time) {
            break; // 如果成立，那么就找到了，  这段时间 所在的 哪一个item
          }
        }

        if (this.$store.state.playerStore.currentLyricIndex !== i - 1) {
          this.$store.commit("playerStore/changeCurrentLyricIndex", i - 1);
          const content =
            this.$store.state.playerStore.lyricList[i - 1] &&
            this.$store.state.playerStore.lyricList[i - 1].content;

          // this.$message({
          //   message: content,
          //   center: true,
          //   duration: 0,
          //   key: "lyric",
          // });
          // 动态更新偏移值
          if (
            this.$store.state.playerStore.currentLyricIndex > 3 ||
            this.$store.state.playerStore.currentLyricIndex <
              this.$store.state.playerStore.lyricList.length - 3
          ) {
            this.lyricY =
              -30 * (this.$store.state.playerStore.currentLyricIndex - 2);
          }
        }
      }
    },
    // 音频标签 播放歌曲 结束
    songAudioEnd() {},
    //歌曲实时播放进度 ,slider，鼠标在上面点击，并且松手了
    songProgressChange(number) {
      this.songProgressIsChanging = false;

      // console.log("change", number);
      this.realDuration = moment((number / 100) * this.dt).format("mm:ss");

      this.$refs.songAudioRef.currentTime = ((number / 100) * this.dt) / 1000;

      this.songProgress = number;
    },
    // 音频 大小的 slider ，鼠标在上面滑动的时候
    voiceProgressInput(number) {
      if (number == 0) {
        this.jingyinFlag = true;
      } else {
        this.jingyinFlag = false;
      }
      this.voiceProgress = number;
      this.$refs.songAudioRef.volume = number / 100;
    },
    // 音频大小的 slier 鼠标在上面点击后，，鼠标抬起的时候
    voiceProgressChange(number) {
      if (number == 0) {
        this.jingyinFlag = true;
      } else {
        this.jingyinFlag = false;
      }
      this.voiceProgress = number;
      this.$refs.songAudioRef.volume = number / 100;
    },

    //  点击 某歌曲的 播放按钮
    async playSong(item) {
      // 没用打开 player 窗口
      let link = this.$router.resolve({
        path: `/player/${item.id}`, //要跳转的路由
      });
      window.open(link.href, "player"); //打开新窗口
      sessionStorage.setItem("open", true);
    },
    // 格式化时长
    formatDuration(haomiao) {
      return moment(haomiao).format("mm:ss");
    },
    // 格式化作者
    formatAuthor(artists) {
      let authors = "";
      artists.forEach((item) => {
        authors = authors + item.name + " / ";
        // console.log(authors);
      });
      return authors.slice(0, authors.length - 2);
    },

    // // 获取到 歌曲 的 mp3 地址 需要处理的其他逻辑
    // handeleSong() {
    //   // console.dir(this.$refs.songAudioRef);
    //   // this.$refs.songAudioRef
    //   // this.$refs.songAudioRef.volume = 1;
    //   // audioRef.current.volume = 0.2
    //   // 先让  audio 标签  播放歌曲
    //   // this.$refs.songAudioRef.play().then(
    //   //   () => {},
    //   //   (err) => {
    //   //     console.log(err);
    //   //     this.$message.error("亲,此歌曲音频未找到~~~~");
    //   //   }
    //   // );
    // },
  },
  computed: {},

  watch: {
    "$route.params.id": {
      handler: function (newValue, oldValue) {
        if (newValue != oldValue) {
          console.log(newValue, oldValue);
          this.getSongDetails([newValue]);
          this.getSongUrl(newValue);
          this.getSongLyric(newValue);
          this.songProgress = 0;
          this.isPlaying = false;
          this.lyricY = 0;
          this.$store.commit("playerStore/changeCurrentLyricIndex", 0);
        }
      },
      deep: true,
    },
  },
};
</script>

<style lang="less" scoped>
/deep/ .el-checkbox__label {
  display: none;
}
/deep/ .el-checkbox {
  margin-right: 0;
}
/deep/ .el-checkbox__input.is-checked .el-checkbox__inner {
  background-color: @globalColor;
  border-color: @globalColor;
}

// 覆盖 slider 组件
/deep/ .el-slider__bar {
  background-color: @globalColor;
}
/deep/ .el-slider__button {
  border: 2px solid @globalColor;
}

/deep/ .el-image {
  img {
    border-radius: 8px;
  }
}

.player {
  position: relative;
  width: 100%;
  height: 100vh;
  font-size: 14px;
  color: #ffffff;

  audio {
    display: block !important;
    width: 0px;
    height: 0px;
  }

  // 内容
  .player_content {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 10;
    width: 100%;
    height: 100%;
    // logo 和头像
    .logo {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 70px;
      // background-color: red;
      padding-left: 40px;
      padding-right: 40px;
      .logo_left {
        img {
          height: 50px;
          &:hover {
            cursor: pointer;
          }
        }
      }
      .logo_right {
        img {
          width: 40px;
          height: 40px;
          &:hover {
            cursor: pointer;
          }
        }
        span {
          margin-left: 10px;
        }
      }
    }
    // 真正的内容区域
    .player_main {
      position: relative;
      // width: 100%;
      height: calc(100% - 70px);
      // 左边三个的标志
      .player_main_flag {
        // 左侧的三个 链接
        position: absolute;
        top: 10px;
        left: -130px;
        // width: 100px;
        font-size: 18px;
        ul li {
          height: 80px;
          line-height: 80px;
          &:hover {
            cursor: pointer;
          }
        }
      }
      // 歌曲列表和 右边的歌词
      .player_main_content {
        display: flex;
        height: calc(100% - 110px);
        // 歌曲列表板块
        .player_main_content_songsList {
          height: 100%;
          flex: 1;
          // 歌曲列表 板块的 上面的按钮
          .player_main_content_songsList_button {
            padding-bottom: 15px;
            button {
              i {
                margin-right: 5px;
              }
              padding: 10px 14px;
              border-radius: 8px;
              background: none;
              border: 1px solid #aeaeae;
              color: #ffffff;
              margin-right: 10px;
              &:hover {
                border-color: @globalColor;
                color: @globalColor;
              }
            }
          }
          .player_main_content_songsList_list {
            height: calc(100% - 55px);
            overflow-y: auto;
            padding-bottom: 20px;

            // background-color: red;
            .list_item {
              display: flex;
              align-items: center;
              height: 50px;
              color: #ffffff;
              font-size: 14px;
              // background-color: red;
              padding-left: 20px;
              border-radius: 8px;

              &:hover {
                background-color: hsla(0, 0%, 100%, 0.05);
                cursor: pointer;
                .list_item_index {
                  span {
                    display: none;
                  }
                  i {
                    display: block;
                  }
                }
                .list_item_time {
                  span {
                    display: none;
                  }
                  > div {
                    display: block;
                  }
                }
              }
              &:nth-child(1) {
                font-size: 12px;
                color: #999999;
              }
              .list_item_checkbox {
                width: 50px;
              }
              // 索引
              .list_item_index {
                width: 50px;
                i {
                  display: none;
                  &:hover {
                    color: @globalColor;
                  }
                }
              }
              // 歌手
              .list_item_author {
                width: 150px;
                padding-right: 15px;
              }
              .list_item_name {
                flex: 1;
                padding-right: 20px;
              }
              .list_item_time {
                width: 150px;
                // background-color: black;
                > div {
                  display: none;
                  i {
                    margin-right: 15px;
                    &:hover {
                      color: @globalColor;
                    }
                  }
                }
              }
            }
          }
        }

        // 歌词 板块
        .player_main_content_lyric {
          width: 300px;
          height: 100%;
          text-align: center;
          padding: 0 20px;
          .player_main_content_lyric_img {
            position: relative;
            width: 200px;
            height: 200px;
            margin: 0 auto;
            margin-bottom: 20px;
            &::before {
              position: absolute;
              top: 0;
              left: 26px;
              width: 100%;
              height: 100%;
              content: "";
              background: url("../../assets/images/album-cover.png") no-repeat
                center/cover;
            }
          }
          .songs_name {
            margin-top: 20px;
          }
          // 外面的大盒子
          .player_main_content_lyric_box {
            height: calc((100% - 300px) / 2);
            // background-color: red;
            margin-top: 30px;
            overflow: hidden;
          }
          // 真正的歌词包含的盒子
          .player_main_content_lyric_box_content {
            transition: transform 0.1s ease-out 0s;
            // padding-top: 30px;
            p {
              height: 30px;
              line-height: 30px;
              margin-bottom: 0;
              &.active {
                color: @globalColor;
              }
            }
          }

          p {
            margin-bottom: 8px;
            color: #999999;
            span {
              cursor: pointer;
              &:hover {
                color: @globalColor;
              }
            }
          }
        }
      }
      // 播放 等等的一些按钮
      .player_main_audio_button {
        display: flex;
        align-items: center;
        height: 110px;
        // background-color: green;
        .player_main_audio_button_left {
          display: flex;
          align-items: center;
          i {
            font-size: 26px;
            cursor: pointer;
            &:hover {
              color: @globalColor;
            }
          }
          i:nth-child(2) {
            font-size: 50px;
            margin: 0 10px;
          }
        }
        .player_main_audio_button_right {
          display: flex;
          align-items: center;
          .sequence i {
            font-size: 26px;
            margin-right: 10px;
            cursor: pointer;
            &:hover {
              color: @globalColor;
            }
          }
          .slider {
            width: 70px;
          }
        }
        .player_main_audio_button_center {
          display: flex;
          flex-direction: column;
          justify-content: center;
          flex: 1;
          height: 80px;
          padding: 0 30px;
          // background-color: red;
          .player_main_audio_button_center_info {
            display: flex;
            justify-content: space-between;
            i {
              margin: 0 5px;
            }
            .totalDuration {
              color: #959595;
            }
            > div:nth-child(1) {
              span {
                cursor: pointer;
                &:hover {
                  color: @globalColor;
                }
              }
            }
          }
        }
      }
    }
  }
  .player_bg_mask {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.6);
    z-index: 2;
    // transition: background-color 5s ease-out;
  }
  .player_bg {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-repeat: no-repeat;
    background-size: cover;
    background-position: 50%;
    filter: blur(65px);
    background-image: url("https://cdnmusic.migu.cn/picture/2022/0103/0107/AS86260dd164f04b54849343c1e724b7d9.jpg");
    opacity: 0.6;
    transform: translateZ(0);
  }
}
</style>
